﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using MODEL;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 充值系统 订单主表操作
    /// </summary>
    public class OrderMaster
    {
        #region code
        public OrderMaster()
        { }
        /// <summary>
        /// 增加订单信息
        /// </summary>
        /// <param name="model">订单信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool OrderMasterAdd(MODEL.OrderMaster model)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20), 
                                         new SqlParameter("@userid",SqlDbType.VarChar,12),
                                         new SqlParameter("@amount",SqlDbType.Int),
                                         new SqlParameter("@account",SqlDbType.VarChar,50),
                                         new SqlParameter("@payment",SqlDbType.VarChar,50),
                                         new SqlParameter("@gameid",SqlDbType.VarChar,20),
                                         new SqlParameter("@count",SqlDbType.Int),
                                         new SqlParameter("@year",SqlDbType.Char,4),
                                         new SqlParameter("@month",SqlDbType.Char,2),
                                         new SqlParameter("@day",SqlDbType.Char,2),
                                         new SqlParameter("@time",SqlDbType.DateTime),
                                         new SqlParameter("@description",SqlDbType.VarChar,200),
                                         new SqlParameter("@deputyid",SqlDbType.VarChar,20),
                                         new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.OID;
            parameter[1].Value = model.OUSERID;
            parameter[2].Value = model.OAMOUNT;
            parameter[3].Value = model.OACCOUNT;
            parameter[4].Value = model.OPAYMENT;
            parameter[5].Value = model.OGAMEID;
            parameter[6].Value = model.OCOUNT;
            parameter[7].Value = model.OYEAR;
            parameter[8].Value = model.OMONTH;
            parameter[9].Value = model.ODAY;
            parameter[10].Value = model.OTIME;
            parameter[11].Value = model.ODESCRIPTION;
            parameter[12].Value = new DAL.OrderDeputy().GetNewsID();
            for (int i = 0; i < 13; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[13].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProOrdermasterAdd", parameter);
        }
        /// <summary>
        /// 订单主表列表
        /// </summary>
        /// <returns>订单主表列表</returns>
        public DataSet OrderMasterList()
        {
            return DbHelper.RunProcedure("ProOrdermasterSelect");
        }
        /// <summary>
        /// 订单主表列表
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <returns>订单主表列表</returns>
        public DataSet OrderMasterList(string userId)
        {
            SqlParameter[] parameter = { new SqlParameter("@userid", SqlDbType.VarChar, 12) };
            parameter[0].Value = userId;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProOrdermasterSelectByUserid", parameter);
        }

        /// <summary>
        /// 生成新编号
        /// </summary>
        /// <returns></returns>
        public string GetNewsID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(OID) FROM G_ORDERMASTER WHERE OID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }
#endregion
    }
    /// <summary>
    /// 充值系统 订单副表
    /// </summary>
    public class OrderDeputy
    {
        #region code
        public OrderDeputy()
        { }
        /// <summary>
        /// 订单副表信息增加
        /// </summary>
        /// <param name="model">订单副表信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool OrderDeputyAdd(MODEL.OrderDeputy model)
        {
            SqlParameter[] parameter ={ new SqlParameter("@id",SqlDbType.VarChar,20),
                                        new SqlParameter("@orderid",SqlDbType.VarChar,20),
                                        new SqlParameter("@time",SqlDbType.DateTime),
                                        new SqlParameter("@description",SqlDbType.VarChar,200),
                                        new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = GetNewsID();
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.OORDERID;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.OTIME;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.ODESCRIPTION;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProOrderdeputyAdd", parameter);
        }
        /// <summary>
        /// 订单副表列表
        /// </summary>
        /// <returns>订单副表列表</returns>
        public DataSet OrderDeputyList()
        {
            return DbHelper.RunProcedure("ProOrderdeputySelect");
        }
        /// <summary>
        /// 订单副表列表
        /// </summary>
        /// <param name="orderId">订单主表编号</param>
        /// <returns>订单副表列表</returns>
        public DataSet OrderDeputyList(string orderId)
        {
            SqlParameter[] parameter = { new SqlParameter("@orderid", SqlDbType.VarChar, 20) };
            parameter[0].Value = orderId;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProOrderdeputySelectByOrderid", parameter);
        }
        /// <summary>
        /// 生成新编号
        /// </summary>
        /// <returns></returns>
        public string GetNewsID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(OID) FROM G_ORDERDEPUTY WHERE OID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }
#endregion
    }

    public class WalletMaster
    {
        public WalletMaster()
        { }
        public DataSet WalletList()
        {
            return DbHelper.RunProcedure("ProWalletmasterSelect");
        }
        public DataSet WalletList(MODEL.WalletMaster model, string idName)
        {
            string procedureName=string.Empty;
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20) };
            parameter[0].Direction = ParameterDirection.Input;
            switch (idName)
            {
                case "id":
                    {
                        parameter[0].Value = model.UID;
                        procedureName = "ProWalletmasterSelectByID";
                        break;
                    }
                case "gameid":
                    {
                        parameter[0].Value = model.UGAMEID;
                        procedureName = "ProWalletmasterSelectByGameID";
                        break;
                    }
                case "userid":
                    {
                        parameter[0].Value = model.UUSERID;
                        procedureName = "ProWalletmasterSelectByUserID";
                        break;
                    }
                default: break;
            }
            return DbHelper.RunProcedureS(procedureName, parameter);
        }
    }
    public class WalletDeputy
    {
        public WalletDeputy()
        { }
        public bool WalletDeputyAdd(MODEL.WalletDeputy model)
        {
            SqlParameter[] parameter = { new SqlParameter("@id",SqlDbType.VarChar,20),
                                         new SqlParameter("@userid",SqlDbType.VarChar,12),
                                         new SqlParameter("@time",SqlDbType.DateTime),
                                         new SqlParameter("@year",SqlDbType.Char,4),
                                         new SqlParameter("@month",SqlDbType.Char,2),
                                         new SqlParameter("@day",SqlDbType.Char,2),
                                         new SqlParameter("@money",SqlDbType.Int),
                                         new SqlParameter("@gameid",SqlDbType.VarChar,20),
                                         new SqlParameter("@describe",SqlDbType.Text),
                                         new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = GetNewsID();
            parameter[1].Value = model.UUSERID;
            parameter[2].Value = model.UTIME;
            parameter[3].Value = model.UYEAR;
            parameter[4].Value = model.UMONTH;
            parameter[5].Value = model.UDAY;
            parameter[6].Value = model.UMONEY;
            parameter[7].Value = model.UGAMEID;
            parameter[8].Value = model.UDESCRIBE;
            for (int i = 0; i < 9; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[9].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProWalletdeputyAdd", parameter);
        }
        public DataSet WalletDeputyList(string describeStr)
        {
            SqlParameter[] parameter = { new SqlParameter("@describe", SqlDbType.Text) };
            parameter[0].Value = describeStr;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProWalletdeputySelectByDescribe", parameter);
        }
        public DataSet WalletDeputyList(string year, string month, string day)
        {
            SqlParameter[] parameter = { new SqlParameter("@year",SqlDbType.Char,4),
                                         new SqlParameter("@month",SqlDbType.Char,2),
                                         new SqlParameter("@day",SqlDbType.Char,2)};
            parameter[0].Value = year;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = month;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = day;
            parameter[2].Direction = ParameterDirection.Input;

            return DbHelper.RunProcedureS("ProWalletdeputySelectByTime", parameter);
        }
        public DataSet WalletDeputyList(MODEL.WalletDeputy model, string idName)
        {
            string procedureName = string.Empty;
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20) };
            parameter[0].Direction = ParameterDirection.Input;
            switch (idName)
            {
                case "userid":
                    {
                        procedureName = "ProWalletdeputySelectByUserID";
                        parameter[0].Value = model.UUSERID;
                        break;
                    }
                case "gameid":
                    {
                        procedureName = "ProWalletdeputySelectByGameID";
                        parameter[0].Value = model.UGAMEID;
                        break;
                    }
                default: break;
            }
            return DbHelper.RunProcedureS(procedureName, parameter);
        }
        /// <summary>
        /// 生成新编号
        /// </summary>
        /// <returns></returns>
        public string GetNewsID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(UID) FROM G_USERWALLETDEPUTY WHERE UID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }
    }
}
